//
//  LYDBMigrateTool.m
//  Apps
//
//  Created by 李永彬 on 2019/6/5.
//  Copyright © 2019 appscomm. All rights reserved.
//

#import "LYDBMigrateTool.h"

@implementation LYDBMigrateTool

+ (NSString *)sqlAddColumnsName:(NSString *)columnsName
                    columnsType:(NSString *)columnsType
                      tableName:(NSString *)tableName
{
    
    NSString *sql = [NSString stringWithFormat:@"alter table %@ add %@ %@", tableName, columnsName, columnsType];
    return sql;
}

+ (NSString *)sqlDeleteColumns:(NSArray *)columns
                     tableName:(NSString *)tableName
                     tempTable:(NSString *)tempTableName
{
    NSMutableString *colunmsString = [[NSMutableString alloc]init];
    for (int i = 0; i<columns.count; i++) {
        if (i != 0) {
            [colunmsString appendString:@", "];
        }
        [colunmsString appendString:columns[i]];
    }
    
    NSMutableString *sql = [[NSMutableString alloc]init];
    [sql appendString:@"create table "];
    [sql appendFormat:@"%@", tableName];
    [sql appendString:@" as select "];
    [sql appendString:colunmsString];
    [sql appendString:@" from "];
    [sql appendFormat:@"%@", tempTableName];
    
    return sql;
}

+ (NSString *)sqlUpdateOldColumnsValueToNewColumnsValueWithTable:(NSString *)tableName
                                                       oldColumn:(NSString *)oldColumn
                                                       NewColumn:(NSString *)NewColumn
{
    NSString *sql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@", tableName, NewColumn, oldColumn];
    return sql;
}

+ (NSString *)sqlDropTable:(NSString *)tableName
{
    NSString *sql = [NSString stringWithFormat:@"DROP TABLE IF EXISTS %@", tableName];
    return sql;
}

+ (NSString *)sqlRenameOldTable:(NSString *)oldTableName newTable:(NSString *)newTableName
{
    NSString *renameSql = [NSString stringWithFormat:@"ALTER TABLE %@ RENAME TO %@", oldTableName, newTableName];
    return renameSql;
}

+ (NSArray *)filterFromArray:(NSArray *)allArray withOutArray:(NSArray *)filterArray
{
    NSMutableArray *lastArray = [NSMutableArray array];
    for (NSString *string in allArray) {
        if (![filterArray containsObject:string]) {
            [lastArray addObject:string];
        }
    }
    return lastArray;
}

@end
